package Game.Objects;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import GUI.GuiManager;
import Game.GameHelper;
import Game.Items.GameItem;
import Game.Items.Armors.Boots.GIRustyBoots;
import Game.Items.Armors.Chestplates.GIRustyChestplate;
import Game.Items.Armors.Gloves.GIRustyGloves;
import Game.Items.Armors.Helmets.GIRustyHelmet;
import Game.Items.Armors.Shields.GIRustyShield;
import Game.Items.Craft.GIApple;
import Game.Items.Craft.GIBone;
import Game.Items.Craft.GICarrot;
import Game.Items.Craft.GIChickenLeg;
import Game.Items.Craft.GIRatMeat;
import Game.Items.Craft.GISteelIngot;
import Game.Items.Others.GIChestKey;
import Game.Items.Others.GIMap;
import Game.Items.Others.GITorch;
import Game.Items.Potions.GIAntipoisonPotion;
import Game.Items.Potions.GIBigHealthPotion;
import Game.Items.Potions.GIExperiencePotion;
import Game.Items.Potions.GIMagicPotion;
import Game.Items.Potions.GIMediumHealthPotion;
import Game.Items.Potions.GIMydriasisPotion;
import Game.Items.Potions.GIPoisonPotion;
import Game.Items.Potions.GISmallHealthPotion;
import Game.Items.Potions.GIStrengthPotion;
import Game.Items.Recipes.Armors.Boots.GIRBoneBoots;
import Game.Items.Recipes.Armors.Boots.GIRSteelBoots;
import Game.Items.Recipes.Armors.Chestplates.GIRBoneChestplate;
import Game.Items.Recipes.Armors.Chestplates.GIRSteelChestplate;
import Game.Items.Recipes.Armors.Gloves.GIRBoneGloves;
import Game.Items.Recipes.Armors.Gloves.GIRSteelGloves;
import Game.Items.Recipes.Armors.Helmets.GIRBoneHelmet;
import Game.Items.Recipes.Armors.Helmets.GIRSteelHelmet;
import Game.Items.Recipes.Armors.Shields.GIRBoneShield;
import Game.Items.Recipes.Armors.Shields.GIRSteelShield;
import Game.Items.Recipes.Potions.GIRMydriasisPotion;
import Game.Items.Recipes.Potions.GIRSmallHealthPotion;
import Game.Items.Recipes.Weapons.GIRBloodSword;
import Game.Items.Weapons.GIBronzeSword;
import Game.Items.Weapons.GIRustySword;
import Game.Objects.Creatures.GOCreature;
import Tools.SoundManager;

/** Chest class
 * @author Aikidos
 */
public class GOChest extends GameObject {

	/** List of item class (with drop chance) */
	private Map<GameItem, Integer> _lstItems = new HashMap<GameItem, Integer>();
	
	/** Gold */
	private int _gold = 25;
	
	public GOChest(ObjectManager objectManager) throws SlickException {
		super(objectManager, "Chest");
		
		_isCanBeUsed = true;
		
		setSprite(new Image("res/gfx/objects/normal_chest.png"));
		
		// Add items
		
		// Craft
		_lstItems.put(new GISteelIngot(), 50);
		
		// Potions
		_lstItems.put(new GISmallHealthPotion(), 30);
		_lstItems.put(new GIMediumHealthPotion(), 20);
		_lstItems.put(new GIBigHealthPotion(), 10);
		_lstItems.put(new GIAntipoisonPotion(), 20);
		_lstItems.put(new GIStrengthPotion(), 20);
		_lstItems.put(new GIMydriasisPotion(), 20);
		_lstItems.put(new GIExperiencePotion(), 10);
		_lstItems.put(new GIMagicPotion(), 20);
		_lstItems.put(new GIPoisonPotion(), 20);
		
		_lstItems.put(new GITorch(), 50);
		_lstItems.put(new GIMap(), 50);
	}

	@Override
	public void use(GameObject user) throws SlickException,
			InstantiationException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, ClassNotFoundException {
		
		Random rnd = GameHelper.getRandom();
		
		// Player
		if (user.isPlayer()) {
			GOPlayer player = (GOPlayer) user;
			
			boolean isEmpty = true;
			for (Entry<GameItem, Integer> entry : _lstItems.entrySet()) {
				if (rnd.nextInt(1000 - player.getLuck()) <= entry.getValue()) {
					isEmpty = false;
					
					player.getInventory().addItem(entry.getKey());
					
					// Show message
					GuiManager.getMessenger().addMessage("[green]You[/green] picked up a [yellow]" 
							+ entry.getKey().getName() + "[/yellow]");
				}
			}
			
			// Give gold
			int giveGold = rnd.nextInt(_gold + 1);
			if (giveGold > 0) {
				player.Gold += giveGold;
				GuiManager.getMessenger().addMessage("[green]You[/green]"
						+ " picked up a [yellow]Gold[/yellow] x" + giveGold);
			}
			
			// If chest is empty
			if (isEmpty && (giveGold == 0)) {
				// Show message
				GuiManager.getMessenger().addMessage("[yellow]" + getName() + "[/yellow] is empty");
			}
			
			// Play sound
			SoundManager.play("sfx_chest");
		}
		
		// Creature
		if (user.isCreature()) {
			GOCreature creature = (GOCreature) user;
			
			for (Entry<GameItem, Integer> entry : _lstItems.entrySet()) {
				if (rnd.nextInt(1000) <= entry.getValue()) {
					creature.getInventory().addItem(entry.getKey());
				}
			}
			
			// Give gold
			int giveGold = rnd.nextInt(_gold + 1);
			if (giveGold > 0) {
				creature.Gold += giveGold;
			}
		}
		
		_objectManager.destroyObject(this);
		
	}

}
